package com.sitechasia.webx.components.xtable.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.FilterSet;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;

import com.ce.webframework.sec.vo.SecUserinfoVO;
import com.sitechasia.webx.core.support.Page;


public class ExtremeUtils {

	public static final String DEFAULT_ROWDISPLAYED = "15";


	/**
	 * 获取列表的limit,然后可调用 limit.getSort(); limit.getFilterSet();方法进行排序、查找操作
	 *
	 * @param request
	 * @return
	 */
	public static Limit getLimit(HttpServletRequest request) {

		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		Limit limit = new TableLimit(limitFactory);
		return limit;
	}

	/**
	 * 获取列表的sort
	 *
	 *
	 * @return
	 */
	public static Sort getSort(HttpServletRequest request) {

		Limit limit = getLimit(request);
		Sort sort = limit.getSort();
		return sort;
	}

	/**
	 * 获取列表的FilterSet
	 *
	 *
	 * @return
	 */
	public static FilterSet getFilterSet(HttpServletRequest request) {

		Limit limit = getLimit(request);
		FilterSet fs = limit.getFilterSet();
		return fs;
	}

	/**
	 * 当列表中的属性与数据库中的属性完全匹配时，可调用此方法 取得列表的排序串，当有不匹配项时请调用getSort（）方法，自己实现。
	 *
	 *
	 * @return
	 */

	public static String getOrderString(HttpServletRequest request) {
		String orderStr = "";

		Sort sort = getSort(request);
		if (sort.isSorted()) {
			orderStr = sort.getProperty() + " " + sort.getSortOrder();
		}
		return orderStr;
	}

	/**
	 * 获取排序属性字段名
	 *
	 * @return String
	 */
	public static String getSortProperty(HttpServletRequest request) {
		Sort sort = getSort(request);
		return sort.getProperty();
	}

	/**
	 * 获取排序方式（asc / desc）
	 *
	 * @return String
	 */
	public static String getSortOrder(HttpServletRequest request) {
		Sort sort = getSort(request);
		return sort.getSortOrder();
	}

	/**
	 * 取得当前页数
	 *
	 *
	 * @return
	 */

	public static int getPageNo(HttpServletRequest request) {
		String pageNumParam = request.getParameter("ec_p");
		int pageNo = NumberUtils.toInt(pageNumParam, 1);
		return pageNo;
	}

	/**
	 * 取得每页显示的行数，默认为15
	 *
	 *
	 * @return
	 */
	public static int getPageSize(HttpServletRequest request) {
		String pageSizeParam = request.getParameter("ec_crd");
		System.out.println("pageSizeParam-->"+pageSizeParam);
		if (pageSizeParam == null || "".equals(pageSizeParam)) {
			pageSizeParam = DEFAULT_ROWDISPLAYED;
		}

		int nPageSize = Integer.parseInt(pageSizeParam);
		request.setAttribute("ec_crd", nPageSize);
		return nPageSize;
	}

	/**
	 * 功能描述：如果记录小于pageSize就在后面添加空记录
	 * @param obj
	 * @param page
	 * @param request
	 * @return
	 */
	public static List addEmptyRows(Object obj, Page page, HttpServletRequest request){
		List srcList = page.getResult();
		int pageSize = page.getPageSize();//每页显示记录数
		int totalCount = (int)page.getTotalCount();//真实记录数

		request.setAttribute("totalRows", new Integer(totalCount < pageSize ? pageSize:totalCount));
		request.setAttribute("realTotalRows",new Integer(totalCount));//保存真实记录数

		if(srcList.size() < pageSize){
//			System.out.println("userlist.size()--->"+srcList.size());

			int emptySize = (pageSize-srcList.size());
//			System.out.println("emptySize---->"+emptySize);
			for(int i=0;i<emptySize;i++){
//				System.out.println("i--->"+i);
				srcList.add(obj);
			}

		}
		return srcList;
	}

	/**
	 *
	 * <p>功能描述：列表分页
	 * @param sourceList
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public static Page getPageList(List sourceList, Integer pageNo, Integer pageSize){
		long totalCount = sourceList.size();
		long pageCount = totalCount / (long)pageSize;
        if(totalCount % (long)pageSize > 0L)
            pageCount++;
        if((long)pageNo > pageCount)
            pageNo = (int)pageCount;
        if(pageNo == 0)
            pageNo = 1;
        if(totalCount < 1L)
            return new Page(pageSize);
        int startIndex = Page.getStartOfPage(pageNo, pageSize);
        int endIndex = startIndex+pageSize;
        if(endIndex > sourceList.size()){
        	endIndex = sourceList.size();
        }
        List resultList = sourceList.subList(startIndex, endIndex);

		return  new Page(startIndex, endIndex, pageSize, resultList);
	}

}
